//
//  PersonAddCarViewController.m
//  LearningFMDB
//
//  Created by dihuijun on 17/9/13.
//  Copyright © 2017年 Cycle. All rights reserved.
//

#import "PersonAddCarViewController.h"
#import "Person.h"
#import "Car.h"
#import "SQLHelper.h"
@interface PersonAddCarViewController ()<UITableViewDelegate,UITableViewDataSource>


/**   数据源数组   */
@property (nonatomic, strong) NSMutableArray<Car *>*datasource;

/**   表格   */
@property (nonatomic, strong) UITableView *tableView;

@end
static NSString *const ID = @"addPersonCarCell";

@implementation PersonAddCarViewController
/**   表格    */
- (UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}

/**   数据源数组   */
- (NSMutableArray *)datasource{
    if(!_datasource){
        _datasource = [NSMutableArray array];
    }
    return _datasource;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //1.设置导航栏
    [self setupNavgation];
    //2.设置表格
    [self setupTableView];
}
#pragma mark ---- 1.设置导航栏
- (void)setupNavgation{
    self.navigationItem.title = [NSString stringWithFormat:@"%@的所有车",self.person.name];
    self.view.backgroundColor = [UIColor whiteColor];
    //右边添加person
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(ClickAddPersonCar)];
    //获取某人的所有车
    self.datasource = [[SQLHelper sharedSQLHelper] getAllCarsFromPerson:self.person];
    
}
/***
 * 为person添加车车
 */
- (void)ClickAddPersonCar
{
    //1.创建车模型
    Car *car = [[Car alloc]init];
    car.own_id = self.person.ID;
   // car.car_id = @(arc4random_uniform(100));
    //取出车品牌
     NSArray *carArray = [NSMutableArray arrayWithObjects:@"奥迪",@"宝马",@"奔驰",@"本田", nil];
    car.brand = carArray[arc4random_uniform((int)carArray.count)];
    car.price = arc4random_uniform(10000000);
    //2.为某人添加车车
    [[SQLHelper sharedSQLHelper]addCar:car toPerson:self.person];
    //3.获取person所有的车
    self.datasource = [[SQLHelper sharedSQLHelper] getAllCarsFromPerson:self.person];
   // NSLog(@"%@",self.datasource);
    //4.刷新表格
    [self.tableView reloadData];
    
    
}
#pragma mark ---- 2.setupTableView
- (void)setupTableView
{
    //1.add tableView
    [self.view addSubview:self.tableView];

    
    
}



#pragma mark ---- 2.1 表格数据源代理<UITableViewDelegate,UITableViewDataSource>

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datasource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

   //设置数据模型
    Car *car = self.datasource[indexPath.row];
   // NSLog(@"%@ === %@ === %zd===%@",car.brand,car.own_id,car.price,car.car_id);
    cell.textLabel.text = car.brand;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"price: ¥ %zd",car.price];
    
    return cell;
    
}
//删除
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //删除某一行
         Car *car = self.datasource[indexPath.row];
        //执行删除语句
        [[SQLHelper sharedSQLHelper]deleteCar:car fromPerson:self.person];
        //重新查询给数组赋值
        self.datasource = [[SQLHelper sharedSQLHelper]getAllCarsFromPerson:self.person];
        //刷新表格
        [self.tableView reloadData];
    }
}









@end
